package com.innerchic.weqiuqiu.service.old;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.UiModeManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Parcelable;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;

import androidx.core.app.NotificationCompat;
import androidx.core.app.RemoteInput;

import com.innerchic.mapper.log.Logger;
import com.innerchic.sponsor.Sponsor;
import com.innerchic.weqiuqiu.bean.old.MessageMade;
import com.innerchic.weqiuqiu.bean.ServiceStatus;

/**
 * ================================================
 * 作    者：贾恒飞 >>> 17515250730@163.com
 * 项    目：wedudu
 * 日    期：2022/1/11 15:26
 * 包    名：com.innerchic.wedudu.service
 * 描    述：消息读取进程服务
 * Create by zFox from AndroidStudio4.2
 * ================================================
 */
public class WeCoreService extends NotificationListenerService {

    private static boolean startService = false;

    public static boolean isStartService() {
        return startService;
    }

    @SuppressLint("WrongConstant")
    @Override
    public void onListenerConnected() {
        super.onListenerConnected();
        startService = true;

        //发送通知
        ServiceStatus serviceStatus = new ServiceStatus();
        serviceStatus.setServiceIsOpen(true);
        Sponsor.getDefault().post(serviceStatus);
        Logger.d("onListenerConnected()服务启动...");

        //判断是否开启了驾车模式
        boolean isOpenCarMode = ((UiModeManager) getSystemService("uimode")).getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR;
        if (!isOpenCarMode) {
            //开启驾驶模式
            //微信要判断是否开启了驾驶模式,只有开启了才会在service里加东西
            /*
                微信代码摘要:
                ...
                else if (!a.bKN()) {
                    ae.i("MicroMsg.auto.MMAutoMessageReplyReceiver", "not open car mode");
                    AppMethodBeat.o(21162);
                } else if (!a.bKO()) {
                    ae.i("MicroMsg.auto.MMAutoMessageReplyReceiver", "not install auto app");
                    AppMethodBeat.o(21162);
                }
                ...

                public static boolean bKN() {
                    AppMethodBeat.i(198611);
                    if (((UiModeManager) ak.getContext().getSystemService("uimode")).getCurrentModeType() == 3) {
                        AppMethodBeat.o(198611);
                        return true;
                    }
                    AppMethodBeat.o(198611);
                    return false;
                }

                public static boolean bKO() {
                    boolean z;
                    AppMethodBeat.i(21157);
                    try {
                        ak.getContext().getPackageManager().getPackageInfo("com.google.android.projection.gearhead", 1);
                        z = true;
                    } catch (PackageManager.NameNotFoundException e2) {
                        z = false;
                    }
                    ae.i("MicroMsg.auto.AutoLogic", "isInstallAutoApp %b", Boolean.valueOf(z));
                    AppMethodBeat.o(21157);
                    return z;
                }

             */
            UiModeManager uimode = (UiModeManager) getSystemService("uimode");
            uimode.enableCarMode(0);
        }
    }

    @Override
    public void onListenerDisconnected() {
        super.onListenerDisconnected();
        startService = false;
        //通知
        ServiceStatus serviceStatus = new ServiceStatus();
        serviceStatus.setServiceIsOpen(false);
        Sponsor.getDefault().post(serviceStatus);
        Logger.d("onListenerConnected()服务关闭...");
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);
        String packageName = sbn.getPackageName();
        if (!"com.tencent.mm".equals(packageName)) {
            //过滤微信包名
            return;
        }
        Logger.d("onNotificationPosted(" + packageName + ")收到一条消息推送...");
        Notification notification = sbn.getNotification();
        Bundle extras = sbn.getNotification().extras;
        //标题,群聊是[群聊],个人是个人名称
        String title = extras.getString(Notification.EXTRA_TITLE);
        //消息内容
        String content = extras.getString(Notification.EXTRA_TEXT);
        //恒为0
        int smallIconId = extras.getInt(Notification.EXTRA_SMALL_ICON);
        //头像bitmap
        Parcelable parcelable = extras.getParcelable(Notification.EXTRA_LARGE_ICON);
        //获取通知的PendingIntent
        PendingIntent pendingIntent = sbn.getNotification().contentIntent;

        if (title == null || parcelable == null) {
            return;
        }

        //创建消息体
        MessageMade messageMade = new MessageMade();

        //检测是否包含可用回复体
        //微信接收消息类:com.tencent.mm.plugin.auto.service.MMAutoMessageReplyReceiver
        NotificationCompat.CarExtender carExtender = new NotificationCompat.CarExtender(notification);
        NotificationCompat.CarExtender.UnreadConversation unreadConversation = carExtender.getUnreadConversation();
        if (unreadConversation != null) {
            PendingIntent pendingReply = unreadConversation.getReplyPendingIntent();
            RemoteInput remoteInput = unreadConversation.getRemoteInput();
            //key_voice_reply_text
            String key = remoteInput.getResultKey();
            if (pendingReply != null) {
                messageMade.setResultKey(key);
                messageMade.setResultPendingIntent(pendingReply);
            } else {
                Logger.d("回传结果集为null.");
            }
        } else {
            Logger.d("NotificationCompat.CarExtender.UnreadConversation值为null.");
        }

        messageMade.setTitle(title);
        messageMade.setContent(content);
        messageMade.setHeardBitmap(parcelable);
        messageMade.setSmallIconId(smallIconId);
        messageMade.setPendingIntent(pendingIntent);
        Sponsor.getDefault().post(messageMade);
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        super.onNotificationRemoved(sbn);
        Logger.d("onNotificationRemoved()有消息被移除...");
    }

}
